package com.interview.ujiuye1;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Copyright (C), 2018-2020
 * FileName: MyDataAtomic
 * Author:   kongfanyu
 * Date:     2020/10/9 20:21
 */
public class MyDataAtomic {

    AtomicInteger num = new AtomicInteger(0);

    public void addPlusPlus(){
        num.getAndIncrement();//获取并且增加
    }

    public static void main(String[] args) {
        MyDataAtomic myDataAtomic = new MyDataAtomic();
        for (int i=0;i < 20 ; i++){

            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j=0;j <1000;j++){
                        myDataAtomic.addPlusPlus();
                    }
                }
            }).start();
        }

        while (Thread.activeCount() > 2){
            Thread.yield();
        }
        System.out.println(myDataAtomic.num);
    }
}
